﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBD_DAL.Entities;
using BBD_DAL.DBConnector;
using MySql.Data.MySqlClient;
using System.Data;

namespace BBD_DAL.DAO
{
	public class RegionsDao : IBaseDao<Region>
	{
		private DBConnection dbConnector;
		private Dictionary<string, string> queries;

		public RegionsDao(string connectionString)
		{
			this.Initialize(connectionString);
		}

		public void Initialize(string connectionString)
		{
			this.dbConnector = new DBConnection(connectionString);
			queries = new Dictionary<string, string>();
			queries.Add("Get", "SELECT * FROM regiony WHERE region_id = @id;");
			queries.Add("Save", "UPDATE regiony SET region_nazwa = @name WHERE region_id = @id;");
			queries.Add("Insert", "INSERT INTO regiony (region_nazwa) VALUES (@name);");
			queries.Add("Delete", "DELETE FROM regiony WHERE id = @id;");
			queries.Add("DeleteAll", "DELETE FROM regiony; ALTER TABLE regiony AUTO_INCREMENT = 1;");
			queries.Add("List", "SELECT * FROM regiony;");
		}

		public Region Get(long id)
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["Get"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			}, out data);

			List<Region> regions = this.Convert(data);

			return regions != null ? regions[0] : null;
		}

		public void Save(Region entity)
		{
			this.dbConnector.RunQuery(this.queries["Save"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = entity.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@name", Value = entity.RegionCode.GetRegionCode(), MySqlDbType = MySqlDbType.VarChar}
			});
		}

		public Region Insert(Region entity)
		{
			long id;
			this.dbConnector.RunQuery(this.queries["Insert"], new MySqlParameter[]{				
				new MySqlParameter(){ParameterName = "@name", Value = entity.RegionCode.GetRegionCode(), MySqlDbType = MySqlDbType.VarChar}
			}, out id);

			entity.Id = id;

			return entity;
		}

		public void Delete(long id)
		{
			this.dbConnector.RunQuery(this.queries["Delete"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			});
		}

		public void Delete(Region entity)
		{
			this.Delete(entity.Id);
		}

		public void Delete()
		{
			this.dbConnector.RunQuery(this.queries["DeleteAll"], null);
		}

		public List<Region> List()
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["List"], null, out data);
			return this.Convert(data);
		}

		private List<Region> Convert(DataTable data)
		{
			if (data == null || data.Rows == null || data.Rows.Count == 0)
				return null;

			List<Region> regions = new List<Region>();
			foreach (DataRow row in data.Rows)
			{
				Region region = new Region();
				region.Id = Int64.Parse(row["region_id"].ToString());
				region.RegionCode.GetRegionFromCode(row["region_nazwa"].ToString());
				
				regions.Add(region);
			}
			return regions;
		}
	}
}
